define([ 'text!./modal.html', 'app', 'bootstrap', 'jquery-validate-locale' ], function(modalTemplate, app) {
	modalService.$inject = [ '$q', '$rootScope', '$compile', '$sce', '$http' ]
	function modalService($q, $rootScope, $compile, $sce, $http) {
		var alertTemplate = '<span></span>'
		var succ = 'text-success';
		var warn = 'text-warning';
		var error = 'text-danger';
		function provider() {
			var remodal
			var element, options
			this.create = function(ele, opt) {
				element = ele
				options = opt
			}
			this.close = function() {
				element.modal('hide');
			}
			this.destroy = function() {
				element.modal('hide');
			}
			this.show = function() {
				element.modal(options || 'show');
				element.on('hidden.bs.modal', function() {
					element.remove();
				})
			}
			this.onShown = function(fn) {
				element.on('shown.bs.modal', fn)
			}
			this.onClose = function(fn) {
				element.on('hidden.bs.modal', fn)
			}
			return this;
		}
		this.confirm = function(param, option) {
			var q = $q.defer();
			var p = {
				title : '提示',
				titleCls : succ,
				content : '',
				result : '',
				size : 'md',
				isConfirm : true
			}
			if (angular.isObject(param)) {
				angular.extend(p, param)
			} else {
				p.content = param;
			}
			if (p.contentUrl) {
				p.content || (p.content = '<div ng-include="contentUrl"></div>')
			}
			var $scope = $rootScope.$new();
			var element, form, validate
			angular.extend($scope, p)
			var modal = new provider()
			$scope.confirm = function() {
				if (p.validator) {
					var r = p.validator($scope.result)
					if (r != undefined) {
						if (r == false) {
							return;
						}
						if (r.then) {
							r.then(function() {
								q.resolve($scope.result);
								modal.destroy()
							})
							return;
						}
					}
				}
				if (form.length != 0) {
					if (validate.form()) {
						q.resolve($scope.result);
						modal.destroy()
					}
				} else {
					q.resolve($scope.result);
					modal.destroy()
				}
			}
			$scope.cancel = function() {
				q.reject();
				modal.destroy()
				return true;
			}
			element = $compile(modalTemplate)($scope);
			$scope.$$phase || $scope.$apply();
			modal.create(element, option = option || 'show')
			modal.onClose(function() {
				q.reject();
				modal.destroy()
			})
			modal.onShown(function() {
				q.notify(element);
				form = element.find('form')
				if (form.length > 0) {
					validate = form.validate(p.validateRules)
				}
			})

			modal.show()
			if (p.duration) {
				setTimeout(function() {
					modal.destroy()
				}, p.duration);
			}
			q.promise.close = function() {
				modal.destroy()
			}
			q.promise.$scope = $scope
			return q.promise;
		}
		this.show = function(param, option) {
			var p = {
				title : '提示',
				titleCls : succ,
				content : '',
				size : 'md',
				isConfirm : false
			};
			angular.extend(p, param)
			return this.confirm(p, option);
		}
		this.warn = function(content, duration) {
			return this.show({
				content : content,
				titleCls : warn,
				size : 'sm',
				duration : duration
			})
		}
		this.succ = function(content, duration) {
			return this.show({
				content : content,
				titleCls : succ,
				size : 'sm',
				duration : duration
			})
		}
		this.error = function(content, duration) {
			return this.show({
				content : content,
				titleCls : error,
				size : 'sm',
				duration : duration
			})
		}
		return this;
	}
	app.factory('modalService', modalService)
})